package com.shujia.jinjie;

import redis.clients.jedis.Jedis;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
import java.util.Set;

/*
    Redis当作索引存储，mysql存储详细数据（二级索引）


    select * from jd_goods where goods_name="xxxx";
 */
public class AnliDemo3 {
    public static void main(String[] args) throws Exception{
//        createIndexToRedis();

        selectWithPinpai();

    }

    public static void selectWithPinpai() throws Exception{
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要查询的品牌：");
        String pinPai = sc.next();

        //先去redis中将对应品牌的数据id获取到
        Jedis jedis = RedisTool.REDIS_CONN;
        Set<String> ids = jedis.smembers("品牌:" + pinPai);

        Connection conn = MySqlTool.getConnection();
        for (String id : ids) {
            PreparedStatement preparedStatement = conn.prepareStatement("select * from jd_goods where id=?");
            int i = Integer.parseInt(id);
            preparedStatement.setInt(1,i);

            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                String goods_name = resultSet.getString(2);
                String price = resultSet.getString(3);
                String comments = resultSet.getString(4);
                String shop = resultSet.getString(5);
                String icos = resultSet.getString(6);
                System.out.println(goods_name+"|"+price+"|"+comments+"|"+shop+"|"+icos);
            }
        }


        conn.close();
        jedis.close();


    }

    public static void createIndexToRedis() throws Exception{
        //联想| 华为
        //先将对应品牌的id获取存入到redis中
        Connection conn = MySqlTool.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement("select id from jd_goods where goods_name like ?");
        preparedStatement.setString(1,"%HAEWI%");
        ResultSet resultSet = preparedStatement.executeQuery();

        Jedis jedis = RedisTool.REDIS_CONN;

        while (resultSet.next()){
            String id = String.valueOf(resultSet.getInt("id"));
            jedis.sadd("品牌:华为",id);
        }

        jedis.close();


        conn.close();

    }
}
